package config

import (
	"fmt"
	"github.com/urfave/cli/v2"
	"gopkg.in/yaml.v2"
	"os"
)

type App struct {
	BroadcastPort int        `yaml:"broadcast_port" json:"broadcast_port"`
	TcpPort       int        `yaml:"tcp_port" json:"tcp_port"`
	HttpPort      int        `yaml:"http_port" json:"http_port"`
	HttpUser      string     `yaml:"http_user" json:"http_user"`
	HttpPassword  string     `yaml:"http_password" json:"http_password"`
	LogLevel      string     `yaml:"log_level" json:"log_level"`
	DataSource    DataSource `yaml:"data_source" json:"data_source"`
}
type Config struct {
	App App `yaml:"app" json:"app"`
}

var config Config
var initConfig = false

func GetConfig() Config {
	if initConfig {
		return config
	} else {
		panic("config file not init......")
	}

}
func InitConfig(ctx *cli.Context) (Config, error) {
	cfg := ctx.String("cfg")
	if cfg == "" {
		cfg = "./config.yaml"
	}
	if initConfig {
		return config, nil
	}
	fmt.Println("加载配置文件:", cfg)
	yml, err := os.ReadFile(cfg)
	if err != nil {
		return config, err
	}

	err = yaml.Unmarshal(yml, &config)
	if err != nil {
		return config, err
	}
	initConfig = true
	return config, nil
}
